package com.zjitc.web.controller;

import com.zjitc.pojo.Cart;
import com.zjitc.pojo.Page;
import com.zjitc.pojo.Product;
import com.zjitc.pojo.User;
import com.zjitc.service.CartService;
import com.zjitc.service.FavoriteService;
import com.zjitc.service.ShopService;
import com.zjitc.service.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("shop")
public class ShopController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private CartService cartService;
    @Autowired
    private UserSerivce userSerivce;
    @Autowired
    private FavoriteService favoriteService;

    @RequestMapping("home")
    public String show( Model model,HttpSession session){
        Page page = new Page();
        List<Product> products = shopService.queryProductAll(page);
        model.addAttribute("products",products);
        List<Cart> carts=null;
        User user = (User) session.getAttribute("user");
        if (user!=null){
            if (user.getU_id()==0){
                user=userSerivce.queryUserByUsername(user.getUsername());
                session.setAttribute("user",user);
            }
            carts = cartService.queryCartByUserAll(user.getU_id());
            for (Cart cart : carts) {
                cart.setProduct(shopService.queryProductById(cart.getP_id()));
            }
        }
        session.setAttribute("carts",carts);
        return "index";
    }

    @RequestMapping("shop")
    public String shop(Model model, int current_page){
        Page page = shopService.getPage();
        int number = page.getNumber();
        int preDate = page.getCurrent_Date();
        preDate=preDate+(current_page-1)*number;
        page.setCurrent_Date(preDate);
        List<Product> products = shopService.queryProductAll(page);
        model.addAttribute("products",products);
        List<Page> pages = new ArrayList<>();
        for(int i=current_page;i<=(current_page+4);i++){
            if (i>page.getPageTotal()){
                break;
            }
            Page page_now = new Page();
            page_now.setCurrent_page(i);
            pages.add(page_now);
        }
        model.addAttribute("current_page",current_page);
        model.addAttribute("pages",pages);
        return "shop";
    }


    @RequestMapping("favorite")
    public String page(int u_id,Model model){
        List<Integer> p_ids = favoriteService.queryByU_Id(u_id);
        List<Product> products=new ArrayList<>();
        for (int id :p_ids) {
            Product product = shopService.queryProductById(id);
            products.add(product);
        }
        model.addAttribute("products",products);
        return "favorite";
    }

    @RequestMapping("cart")
    public String cart(HttpSession session){
        Object carts = session.getAttribute("carts");
        System.out.println(carts);
        return "cart";
    }

    @RequestMapping("search")
    public String search(String name,Model model){
        System.out.println(name);
        List<Product> products = shopService.queryProductByName(name);
        System.out.println(products);
        model.addAttribute("products",products);
        return "search";
    }

    @RequestMapping("findId")
    @ResponseBody
    public Product findId(int id){
        return shopService.queryProductById(id);
    }
}
